package com.teamsun.mqtt.entity.vo;

import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

import com.teamsun.mqtt.entity.MsgRep;

/**
 * 延迟发送对象
 * @author acer
 *
 */
public class DelaySnedMsg implements  Delayed{

	MsgRep transportMessage; //发送的消息对象
	String clientid;//接受客户端标识
	/**
	 * 将要重发的时间  单位毫秒
	 */
	long  willSendTime;
	
	public DelaySnedMsg(long willSendTime){
		this.willSendTime=willSendTime;
	}
	
	
	public DelaySnedMsg(MsgRep transportMessage, String clientid,
			long willSendTime) {
		super();
		this.transportMessage = transportMessage;
		this.clientid = clientid;
		this.willSendTime = willSendTime;
	}


	@Override
	public int compareTo(Delayed o) {
		
		if(o instanceof  DelaySnedMsg){
			DelaySnedMsg delaySnedMsg=(DelaySnedMsg)o;
			return (int)(this.willSendTime-delaySnedMsg.willSendTime);
		}
		return 1;
	}

	@Override
	public long getDelay(TimeUnit unit) {
		
		long l=new Date().getTime();
		return 	unit.convert(willSendTime-l, TimeUnit.MILLISECONDS);
	}


	public MsgRep getTransportMessage() {
		return transportMessage;
	}


	public void setTransportMessage(MsgRep transportMessage) {
		this.transportMessage = transportMessage;
	}


	public String getClientid() {
		return clientid;
	}


	public void setClientid(String clientid) {
		this.clientid = clientid;
	}
	
	

}
